package edu.drexel.cs.eVoter.VoterClient;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.Map.Entry;

import javax.swing.Box;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class StateResultsPanel extends ClientAbstractPanel 
	implements StateDataListener 
{
	private static final long serialVersionUID = -2539069384666115836L;
	private JLabel    		  stateLabel = new JLabel( "Select State:" );
	private JComboBox<String> stateComboBox;
	private JTable    		  stateTable;
	private Vector<String>    columns;
	
	private Vector<Vector<String>>    rowData;
	
	private String[]          stateStrings = {"Alabama","Alaska","American Samoa","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","District of Columbia","Florida","Georgia","Guam","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina","North Dakota","Northern Marianas Islands ","Ohio","Oklahoma","Oregon","Pennsylvania","Puerto Rico","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia ","Virgin Islands ","Washington","West Virginia","Wisconsin","Wyoming "};;
	//private String[]          candidateStrings = {"Mitt Romney", "Barack Obama", "Iron Man", "Hulk", "Jane Doe"};
	//private String[]          voterStrings = { "Somebody 1", "Somebody 2", "Somebody 3", "Somebody 4", "Somebody 5" };
	
	public StateResultsPanel(ServerInterface si) 
	{
		super(si);

		si.addStateDataListener( this );
		Box mainbox, b;
		
		mainbox = Box.createVerticalBox();
		
		b = Box.createHorizontalBox();
		b.add( stateLabel );
		stateComboBox = new JComboBox<String>( stateStrings );
		stateComboBox.addActionListener( new ActionListener() 
		{	
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				processStateSelection( e );
			}
		});
		b.add( stateComboBox );
		mainbox.add( b );
		
		columns = new Vector<String>();
		columns.add( "Candidates" );
		columns.add( "Votes" );
		rowData = new Vector<Vector<String>>();
		rowData.add( new Vector<String>(2) );
		rowData.add( new Vector<String>(2) );
		rowData.add( new Vector<String>(2) );
		rowData.get(0).add( "No Data" );
		rowData.get(0).add( "0" );
		rowData.get(1).add( "No Data" );
		rowData.get(1).add( "No Data" );
		rowData.get(2).add( "No Data" );
		rowData.get(2).add( "0" );
		
		stateTable = new JTable( rowData, columns );
		stateTable.setEnabled(false);
		
		JScrollPane jsp;
		jsp = new JScrollPane( stateTable );
		
		mainbox.add( jsp );
		add( mainbox );
	}

	private void processStateSelection( ActionEvent e )
	{
		myServerInterface.stateResults( stateComboBox.getSelectedItem().toString() );
	}
	
	@Override
	public void stateData(Map<String, Integer> data) 
	{
		// update the rowData
		Set<Entry<String,Integer>> set = data.entrySet();
		
		Iterator<Entry<String, Integer>> iter = set.iterator();
		
		rowData.clear();
		
		while ( iter.hasNext() )
		{
			Entry<String, Integer> ent = iter.next();
			
			String candidate = ent.getKey();
			Integer votes = ent.getValue();
			
			System.out.println("\n\ncandidate = <" + candidate + "> and votes = <" + votes + ">");
			Vector<String> temp = new Vector<String>(2);
			temp.add( candidate );
			temp.add( votes.toString() );
			
			rowData.add( temp );
		}
		
		stateTable.setModel(new DefaultTableModel( rowData, columns ) );
		
		System.out.println("Update Completed in stateData, size was " + set.size() + " and " + rowData.size() );
	}
}
